Change in openvpn[master]: options: Factor out usages of strtoll and atoll
A message that is not in the archive (sent off-list or not yet received): gerrit.1755877911000.I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61@...2715...
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email
to review the following change.
Change subject: options: Factor out usages of strtoll and atoll
......................................................................
options: Factor out usages of strtoll and atoll
This covers the cases where we actually want to
allow numbers > 2^31
---
M src/openvpn/options.c
M src/openvpn/options_util.c
M src/openvpn/options_util.h
M tests/unit_tests/openvpn/test_misc.c
4 files changed, 41 insertions(+), 12 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1154/1
src/openvpn/options.c +3 -12
@@ -7181,8 +7181,7 @@ options->inactivity_timeout =positive_atoi (p[1 ], msglevel);if (p[2 ]) { -int64_t val =atoll (p[2 ]); - options->inactivity_minimum_bytes = (val <0 ) ?0 : val; +positive_atoll (p[2 ], &options->inactivity_minimum_bytes, p[0 ], msglevel);if (options->inactivity_minimum_bytes >INT_MAX ) { msg(M_WARN,
@@ -9528,26 +9527,18 @@else if (streq (p[0 ],"reneg-bytes" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long reneg_bytes =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || reneg_bytes <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_bytes, p[0 ], msglevel)) { -msg (msglevel,"--reneg-bytes parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_bytes = reneg_bytes; }else if (streq (p[0 ],"reneg-pkts" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long pkt_max =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || pkt_max <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_packets, p[0 ], msglevel)) { -msg (msglevel,"--reneg-pkts parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_packets = pkt_max; }else if (streq (p[0 ],"reneg-sec" ) && p[1 ] && !p[3 ]) {
src/openvpn/options_util.c +16 -0
@@ -131,6 +131,22 @@return (int )i; } +bool +positive_atoll (const char *str,int64_t *value,const char *name,int msglevel) +{ +char *endptr; +long long ll =strtoll (str, &endptr,10 ); + +if (ll <0 || *endptr !='\0' ) + { +msg (msglevel,"%s: Cannot parse '%s' as non-negative integer" , name, str); +return false; + } + + *value = (int64_t )ll; +return true; +} +int atoi_warn (const char *str,int msglevel) {
src/openvpn/options_util.h +11 -0
@@ -41,6 +41,17 @@/** * Converts a str to an integer if the string can be represented as an +* integer number and is >= 0. +* The integer is stored in \p value. +* On error, print a warning with \p msglevel using \p name. \p value is +* not changed on error. +* +* @return \c true if the integer has been parsed and stored in value, \c false otherwise +*/ +bool positive_atoll (const char *str,int64_t *value,const char *name,int msglevel); + +/** +* Converts a str to an integer if the string can be represented as an * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn (const char *str,int msglevel);
tests/unit_tests/openvpn/test_misc.c +11 -0
@@ -359,6 +359,17 @@assert_true (atoi_constrained ("-1194" , ¶meter,"test" ,INT_MIN ,INT_MAX , msglevel));assert_int_equal (parameter,-1194 ); +int64_t parameter64 =0 ; +assert_true (positive_atoll ("1234" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,1234 ); +assert_true (positive_atoll ("0" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,0 ); +assert_true (positive_atoll ("2147483653" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,2147483653 ); +/* overflow gets capped to LLONG_MAX */ +assert_true (positive_atoll ("9223372036854775810" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,9223372036854775807 ); +CLEAR (mock_msg_buf);assert_int_equal (positive_atoi ("-1234" , msglevel),0 );assert_string_equal (mock_msg_buf,"Cannot parse argument '-1234' as non-negative integer" );
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61
Gerrit-Change-Number: 1154
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-MessageType: newchange
Attention is currently required from: flichtenheld, plaisthos.
MaxF has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email )
Change subject: options: Factor out usages of strtoll and atoll
......................................................................
Patch Set 4: Code-Review+2
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61
Gerrit-Change-Number: 1154
Gerrit-PatchSet: 4
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 11 Sep 2025 15:49:15 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
PATCH v4 1/1 options: Factor out usages of strtoll and atoll
In reply to a message not in the archiveFrom: Frank Lichtenheld <[email protected]...>
This covers the cases where we actually want to
allow numbers > 2^31
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1154
This mail reflects revision 4 of this Change.
Acked-by according to Gerrit (reflected above):
MaxF <[email protected]...>
src/openvpn/options.c +3 -12
@@ -7185,8 +7185,7 @@ options->inactivity_timeout =positive_atoi (p[1 ], msglevel);if (p[2 ]) { -int64_t val =atoll (p[2 ]); - options->inactivity_minimum_bytes = (val <0 ) ?0 : val; +positive_atoll (p[2 ], &options->inactivity_minimum_bytes, p[0 ], msglevel);if (options->inactivity_minimum_bytes >INT_MAX ) { msg(M_WARN,
@@ -9541,26 +9540,18 @@else if (streq (p[0 ],"reneg-bytes" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long reneg_bytes =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || reneg_bytes <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_bytes, p[0 ], msglevel)) { -msg (msglevel,"--reneg-bytes parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_bytes = reneg_bytes; }else if (streq (p[0 ],"reneg-pkts" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long pkt_max =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || pkt_max <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_packets, p[0 ], msglevel)) { -msg (msglevel,"--reneg-pkts parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_packets = pkt_max; }else if (streq (p[0 ],"reneg-sec" ) && p[1 ] && !p[3 ]) {
src/openvpn/options_util.c +16 -0
@@ -131,6 +131,22 @@return (int )i; } +bool +positive_atoll (const char *str,int64_t *value,const char *name,int msglevel) +{ +char *endptr; +long long ll =strtoll (str, &endptr,10 ); + +if (ll <0 || *endptr !='\0' ) + { +msg (msglevel,"%s: Cannot parse '%s' as non-negative integer" , name, str); +return false; + } + + *value = (int64_t )ll; +return true; +} +int atoi_warn (const char *str,int msglevel) {
src/openvpn/options_util.h +11 -0
@@ -41,6 +41,17 @@/** * Converts a str to an integer if the string can be represented as an +* integer number and is >= 0. +* The integer is stored in \p value. +* On error, print a warning with \p msglevel using \p name. \p value is +* not changed on error. +* +* @return \c true if the integer has been parsed and stored in value, \c false otherwise +*/ +bool positive_atoll (const char *str,int64_t *value,const char *name,int msglevel); + +/** +* Converts a str to an integer if the string can be represented as an * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn (const char *str,int msglevel);
tests/unit_tests/openvpn/test_misc.c +11 -0
@@ -359,6 +359,17 @@assert_true (atoi_constrained ("-1194" , ¶meter,"test" ,INT_MIN ,INT_MAX , msglevel));assert_int_equal (parameter,-1194 ); +int64_t parameter64 =0 ; +assert_true (positive_atoll ("1234" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,1234 ); +assert_true (positive_atoll ("0" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,0 ); +assert_true (positive_atoll ("2147483653" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,2147483653 ); +/* overflow gets capped to LLONG_MAX */ +assert_true (positive_atoll ("9223372036854775810" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,9223372036854775807 ); +CLEAR (mock_msg_buf);assert_int_equal (positive_atoi ("-1234" , msglevel),0 );assert_string_equal (mock_msg_buf,"Cannot parse argument '-1234' as non-negative integer" );
[Openvpn-devel] [PATCH applied] Re: options: Factor out usages of strtoll and atoll
Change looks reasonable, MaxF has ACKed it, the BBs confirm nothing breaks, and I have actually very lightly tested it (--reneg-pkts, --reneg-bytes, with easily-achievable values - watch out for DCO...).
For values >2^63, strtoll() truncates to 9223372036854775808 - which is documented and tested for. We do not error on this today, but for what it's used for, clamping is probably fine (because larger numbers make even less sense).
Your patch has been applied to the master branch.
This time, mail-archive.org (for a change) actually acknowledges existance of the mail with the patch... yay.
commit 10c42c7407ccc467f75e25f752275e412ff35a91
Author: Frank Lichtenheld Date: Thu Sep 11 22:15:00 2025 +0200
options: Factor out usages of strtoll and atoll
Signed-off-by: Frank Lichtenheld <[email protected]...>
Acked-by: MaxF <[email protected]...>
URL: https://gerrit.openvpn.net/c/openvpn/+/1154
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg32858.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
cron2 has uploaded a new patch set (#5) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email )
The following approvals got outdated and were removed:
Code-Review+2 by MaxF
Change subject: options: Factor out usages of strtoll and atoll
......................................................................
options: Factor out usages of strtoll and atoll
This covers the cases where we actually want to
allow numbers > 2^31
URL: https://gerrit.openvpn.net/c/openvpn/+/1154
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg32858.html
---
M src/openvpn/options.c
M src/openvpn/options_util.c
M src/openvpn/options_util.h
M tests/unit_tests/openvpn/test_misc.c
4 files changed, 41 insertions(+), 12 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/54/1154/5
src/openvpn/options.c +3 -12
@@ -7193,8 +7193,7 @@ options->inactivity_timeout =positive_atoi (p[1 ], msglevel);if (p[2 ]) { -int64_t val =atoll (p[2 ]); - options->inactivity_minimum_bytes = (val <0 ) ?0 : val; +positive_atoll (p[2 ], &options->inactivity_minimum_bytes, p[0 ], msglevel);if (options->inactivity_minimum_bytes >INT_MAX ) { msg(M_WARN,
@@ -9545,26 +9544,18 @@else if (streq (p[0 ],"reneg-bytes" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long reneg_bytes =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || reneg_bytes <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_bytes, p[0 ], msglevel)) { -msg (msglevel,"--reneg-bytes parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_bytes = reneg_bytes; }else if (streq (p[0 ],"reneg-pkts" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long pkt_max =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || pkt_max <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_packets, p[0 ], msglevel)) { -msg (msglevel,"--reneg-pkts parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_packets = pkt_max; }else if (streq (p[0 ],"reneg-sec" ) && p[1 ] && !p[3 ]) {
src/openvpn/options_util.c +16 -0
@@ -131,6 +131,22 @@return (int )i; } +bool +positive_atoll (const char *str,int64_t *value,const char *name,int msglevel) +{ +char *endptr; +long long ll =strtoll (str, &endptr,10 ); + +if (ll <0 || *endptr !='\0' ) + { +msg (msglevel,"%s: Cannot parse '%s' as non-negative integer" , name, str); +return false; + } + + *value = (int64_t )ll; +return true; +} +int atoi_warn (const char *str,int msglevel) {
src/openvpn/options_util.h +11 -0
@@ -41,6 +41,17 @@/** * Converts a str to an integer if the string can be represented as an +* integer number and is >= 0. +* The integer is stored in \p value. +* On error, print a warning with \p msglevel using \p name. \p value is +* not changed on error. +* +* @return \c true if the integer has been parsed and stored in value, \c false otherwise +*/ +bool positive_atoll (const char *str,int64_t *value,const char *name,int msglevel); + +/** +* Converts a str to an integer if the string can be represented as an * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn (const char *str,int msglevel);
tests/unit_tests/openvpn/test_misc.c +11 -0
@@ -359,6 +359,17 @@assert_true (atoi_constrained ("-1194" , ¶meter,"test" ,INT_MIN ,INT_MAX , msglevel));assert_int_equal (parameter,-1194 ); +int64_t parameter64 =0 ; +assert_true (positive_atoll ("1234" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,1234 ); +assert_true (positive_atoll ("0" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,0 ); +assert_true (positive_atoll ("2147483653" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,2147483653 ); +/* overflow gets capped to LLONG_MAX */ +assert_true (positive_atoll ("9223372036854775810" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,9223372036854775807 ); +CLEAR (mock_msg_buf);assert_int_equal (positive_atoi ("-1234" , msglevel),0 );assert_string_equal (mock_msg_buf,"Cannot parse argument '-1234' as non-negative integer" );
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61
Gerrit-Change-Number: 1154
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email )
Change subject: options: Factor out usages of strtoll and atoll
......................................................................
options: Factor out usages of strtoll and atoll
This covers the cases where we actually want to
allow numbers > 2^31
URL: https://gerrit.openvpn.net/c/openvpn/+/1154
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg32858.html
---
M src/openvpn/options.c
M src/openvpn/options_util.c
M src/openvpn/options_util.h
M tests/unit_tests/openvpn/test_misc.c
4 files changed, 41 insertions(+), 12 deletions(-)
src/openvpn/options.c +3 -12
@@ -7193,8 +7193,7 @@ options->inactivity_timeout =positive_atoi (p[1 ], msglevel);if (p[2 ]) { -int64_t val =atoll (p[2 ]); - options->inactivity_minimum_bytes = (val <0 ) ?0 : val; +positive_atoll (p[2 ], &options->inactivity_minimum_bytes, p[0 ], msglevel);if (options->inactivity_minimum_bytes >INT_MAX ) { msg(M_WARN,
@@ -9545,26 +9544,18 @@else if (streq (p[0 ],"reneg-bytes" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long reneg_bytes =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || reneg_bytes <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_bytes, p[0 ], msglevel)) { -msg (msglevel,"--reneg-bytes parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_bytes = reneg_bytes; }else if (streq (p[0 ],"reneg-pkts" ) && p[1 ] && !p[2 ]) {VERIFY_PERMISSION (OPT_P_TLS_PARMS ); -char *end; -long long pkt_max =strtoll (p[1 ], &end,10 ); -if (*end !='\0' || pkt_max <0 ) +if (!positive_atoll (p[1 ], &options->renegotiate_packets, p[0 ], msglevel)) { -msg (msglevel,"--reneg-pkts parameter must be an integer and >= 0" ); goto err; } - options->renegotiate_packets = pkt_max; }else if (streq (p[0 ],"reneg-sec" ) && p[1 ] && !p[3 ]) {
src/openvpn/options_util.c +16 -0
@@ -131,6 +131,22 @@return (int )i; } +bool +positive_atoll (const char *str,int64_t *value,const char *name,int msglevel) +{ +char *endptr; +long long ll =strtoll (str, &endptr,10 ); + +if (ll <0 || *endptr !='\0' ) + { +msg (msglevel,"%s: Cannot parse '%s' as non-negative integer" , name, str); +return false; + } + + *value = (int64_t )ll; +return true; +} +int atoi_warn (const char *str,int msglevel) {
src/openvpn/options_util.h +11 -0
@@ -41,6 +41,17 @@/** * Converts a str to an integer if the string can be represented as an +* integer number and is >= 0. +* The integer is stored in \p value. +* On error, print a warning with \p msglevel using \p name. \p value is +* not changed on error. +* +* @return \c true if the integer has been parsed and stored in value, \c false otherwise +*/ +bool positive_atoll (const char *str,int64_t *value,const char *name,int msglevel); + +/** +* Converts a str to an integer if the string can be represented as an * integer number. Otherwise print a warning with \p msglevel and return 0 */ int atoi_warn (const char *str,int msglevel);
tests/unit_tests/openvpn/test_misc.c +11 -0
@@ -359,6 +359,17 @@assert_true (atoi_constrained ("-1194" , ¶meter,"test" ,INT_MIN ,INT_MAX , msglevel));assert_int_equal (parameter,-1194 ); +int64_t parameter64 =0 ; +assert_true (positive_atoll ("1234" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,1234 ); +assert_true (positive_atoll ("0" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,0 ); +assert_true (positive_atoll ("2147483653" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,2147483653 ); +/* overflow gets capped to LLONG_MAX */ +assert_true (positive_atoll ("9223372036854775810" , ¶meter64,"test" , msglevel)); +assert_int_equal (parameter64,9223372036854775807 ); +CLEAR (mock_msg_buf);assert_int_equal (positive_atoi ("-1234" , msglevel),0 );assert_string_equal (mock_msg_buf,"Cannot parse argument '-1234' as non-negative integer" );
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1154?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I454126b3f8fa9d14501f6c4b1ed9ce7b2904be61
Gerrit-Change-Number: 1154
Gerrit-PatchSet: 5
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: MaxF <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged